//
//  CourseViewController.m
//  Arts1
//
//  Created by lanou on 15/8/28.
//  Copyright (c) 2015年 Arts1. All rights reserved.


#import "CourseViewController.h"
#import "AllHeader.h"
#import "SearchBarView.h"
#import "CourseMainCollectionViewController.h"
#import "CourseCollectionViewCell.h"
#import "CourseDetailViewController.h"

#import "CourseCateModel.h" // 分类
#import "CourseModel.h"



// 课程分类的接口
#define couCatURL @"http://arts1.dlab.com.cn/app/cat"

/* *************************************
 * 横向滑动的collectionview建立在该控制器内，纵向滑动的视图控制器是CourseMainCollectionViewController
 * 头部的搜索框 是SearchBarView
 */

@interface CourseViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate>

// 搜索栏
@property (nonatomic, strong) SearchBarView *searchBar;

// 横向滑动的collectionview
@property (nonatomic, strong) UICollectionView *collectionview;

// 纵向滑动的collectionview
@property (nonatomic, strong) CourseMainCollectionViewController *mainCollectionVC;

@property (nonatomic ,strong) UIView *whiteview;

// 课程分类数据源
@property (nonatomic, strong) NSMutableArray *dataArr;



@end

@implementation CourseViewController

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        CourseCateModel *cate = [[CourseCateModel alloc] init];
        cate.catName = @"全部";
        cate.foreignName = @"All";
        self.dataArr = [NSMutableArray arrayWithObject:cate];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    self.navigationController.navigationBarHidden = YES;
    // Do any additional setup after loading the view.
    
    [self request];
    [self createMaincollectionview];
    [self createCourseCollectionView];
    
}
// 网络请求
- (void)request{
        AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
        manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
        manage.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manage POST:couCatURL parameters:nil
             success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             // 请求成功的时候调用这个block
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             //返回的状态
             int index = [dic[@"status"] intValue];
             if (index == 1) {
                 NSArray *arr = dic[@"list"];
                 for (NSDictionary *dict in arr) {
                     CourseCateModel *cate = [[CourseCateModel alloc] init];
                     [cate setValuesForKeysWithDictionary:dict];
                     [self.dataArr addObject:cate];
                 }
             }
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.collectionview reloadData];
             });
             
             NSLog(@"课程分类返回数据 ： %@", dic);
             
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
        
}
    





// 创建课程分类的视图
- (void)createCourseCollectionView{
    _whiteview = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH , (SCREEN_WIDTH - 50)/4 + 64)];
    _whiteview.backgroundColor = [UIColor whiteColor];
    // 搜索框
    _searchBar = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 20 , SCREEN_WIDTH , 44)];
    _searchBar.searchBar.delegate = self;
    [_whiteview addSubview:_searchBar];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5 , SCREEN_WIDTH , 0.5)];
    line.backgroundColor = kCustomColor(150, 150, 150, 0.5);
    [_whiteview addSubview:line];
    
    // 视频分类视图
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(8, 10, 8, 10);
    _collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, (SCREEN_WIDTH - 50)/4 + 16) collectionViewLayout:layout];
    _collectionview.backgroundColor = [UIColor whiteColor];
    _collectionview.delegate = self;
    _collectionview.dataSource = self;
    _collectionview.alwaysBounceHorizontal = NO;
    _collectionview.showsHorizontalScrollIndicator = NO;
    [_collectionview registerClass:[CourseCollectionViewCell class] forCellWithReuseIdentifier:@"coursecollectioncell"];
    
     [_whiteview addSubview:_collectionview];
    
    [UIView viewWideForShadowColor:_whiteview];
   
    [self.view addSubview:_whiteview];
    
}




// 创建课程列表的展示视图

- (void)createMaincollectionview{
    // 创建flowlayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumInteritemSpacing = 10;
    
    // 创建课程展示页面，并修改view的frame
    _mainCollectionVC = [[CourseMainCollectionViewController alloc] initWithCollectionViewLayout:layout];
    [self.view addSubview:_mainCollectionVC.collectionView];
    
   _mainCollectionVC.collectionView.frame = CGRectMake(0, (SCREEN_WIDTH - 50)/4 + 54 + 6, SCREEN_WIDTH , SCREEN_HEIGHT - (SCREEN_WIDTH - 50)/4 - 54);
    
    // 实现 CourseMainCollectionViewController 的block，跳转到详情页面
      __weak CourseViewController *wcourseVC = self;
    _mainCollectionVC.playcourseblock = ^(CourseModel * model){
      CourseDetailViewController *courseDVC = [[CourseDetailViewController alloc] init];
        courseDVC.model = model;
        NSLog(@" 进入视频详情 model.Id : %@", model.Id);
        [wcourseVC.navigationController pushViewController:courseDVC animated:YES];
           };
}


#pragma mark -- UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH - 50)/4, (SCREEN_WIDTH - 50)/4);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"coursecollectioncell" forIndexPath:indexPath];
    cell.cate = (CourseCateModel *)_dataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CourseCateModel *cate = (CourseCateModel *)_dataArr[indexPath.row];
    _mainCollectionVC.catId = cate.Id;
}

#pragma mark --- UISearchBarDelegate
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [_searchBar.searchBar resignFirstResponder];
}



// 隐藏搜索框的键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
